﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

using LiveCharts;
using LiveCharts.Wpf;
using LiveCharts.Configurations;

namespace charts222
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        /* SectionsCollection 和
         * SeriesCollection这两个不要搞混乱了，用的是这一行的
         * 
         * public SeriesCollection SeriesCollection { get; set; }
         * public SeriesCollection SectionsX { get; set; }*/

        //public SeriesCollection SeriesCollection { get; set; }
        public ChartValues<double> OccupancyRatesCell1 { get; set; }
        public ChartValues<double> OccupancyRatesCell2 { get; set; }
        public ChartValues<double> OccupancyRatesCell3 { get; set; }

        public SeriesCollection SeriesCollection { get; set; }
        public List<string> Labels { get; set; }
        private double _trend;
        private double _trend2;
        private double _trend3;
        private double[] temp = { 1, 3, 2, 4, -3, 5, 2, 1 };
        public MainWindow()
        {
            InitializeComponent();
            OccupancyRatesCell1 = new ChartValues<double> { 0.75, 0.87, 0.82, 0.69, 0.73, 0.71, 0.80, 0.85, 0.88, 0.79 };//将数据显示到图表
            OccupancyRatesCell2 = new ChartValues<double> { 1.75, 1.87, 1.82, 1.69, 1.73, 1.71, 1.80, 1.85, 1.88, 1.79 };//将数据显示到图表
            OccupancyRatesCell3 = new ChartValues<double> { 2.75, 2.87, 2.82, 2.69, 2.73, 2.71, 2.80, 2.85, 2.88, 2.79 };//将数据显示到图表
            ////实例化一条折线图
            //LineSeries mylineseries = new LineSeries();
            ////设置折线的标题
            //mylineseries.Title = "Temp";
            ////折线图直线形式
            //mylineseries.LineSmoothness = 0;
            ////折线图的无点样式
            //mylineseries.PointGeometry = null;
            ////添加横坐标
            Labels = new List<string> { "1", "3", "2", "4", "0", "5", "2", "1" };
            ////添加折线图的数据
            //mylineseries.Values = new ChartValues<double>(temp);
            //SeriesCollection = new SeriesCollection { };
            //SeriesCollection.Add(mylineseries);
            //_trend = 8;
            //linestart();
            LineX();
            DataContext = this;
        }


        private void Button_AddTest_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Test_Click(object sender, RoutedEventArgs e)
        {
            LineX();
        }

        //连续折线图的方法
        public void linestart()
        {
            Task.Run(() =>
            {
                var r = new Random();
                while (true)
                {
                    Thread.Sleep(500);
                    _trend = r.Next(0, 10);
                    //通过Dispatcher在工作线程中更新窗体的UI元素
                    Application.Current.Dispatcher.Invoke(() =>
                {
                        //更新横坐标时间
                    Labels.Add(DateTime.Now.ToString("HH:mm:ss"));
                    Labels.RemoveAt(0);
                        //更新纵坐标数据
                    SeriesCollection[0].Values.Add(_trend);
                    SeriesCollection[0].Values.RemoveAt(0);
                });
                }
            });
        }

        public void LineX()
        {
            Task.Run(() =>
            {
                var r = new Random();
            while (true)
            {
                Thread.Sleep(500);
                    _trend = r.Next(0, 4);
                _trend2 = r.Next(3, 6);
                _trend3 = r.Next(5, 10);
                //通过Dispatcher在工作线程中更新窗体的UI元素
                Application.Current.Dispatcher.Invoke(() =>
                    {
                        //更新横坐标时间
                        Labels.Add(DateTime.Now.ToString("HH:mm:ss"));
                        Labels.RemoveAt(0);
                        //更新纵坐标数据

                        OccupancyRatesCell1.Add(_trend);
                        OccupancyRatesCell1.RemoveAt(0);

                        OccupancyRatesCell2.Add(_trend2);
                        OccupancyRatesCell2.RemoveAt(0);

                        OccupancyRatesCell3.Add(_trend3);
                        OccupancyRatesCell3.RemoveAt(0);
                        //OccupancyRatesCell1.Remove(0);
                        //SeriesCollection[0].Values.Add(_trend);
                        //SeriesCollection[0].Values.RemoveAt(0);
                    });
                }
            });
        }

     
    }
}
